<div id="dialog-new-doc" class="form-horizontal">
    <div class="modal-body">
        <div class="form-group">
            <label class="col-sm-2 control-label">名字</label>
            <div class="col-sm-10">
                <input class="form-control" type="text" name="name" style="width: 100%"/>
            </div>
            <div class="clearfix"></div>
        </div>
        <!--        <div class="form-group">-->
        <!--            <label class="col-sm-2 control-label">描述</label>-->
        <!--            <div class="col-sm-10">-->
        <!--                <input class="form-control" type="text" name="content" style="width: 100%"/>-->
        <!--            </div>-->
        <!--            <div class="clearfix"></div>-->
        <!--        </div>-->
    </div>
    <div class="modal-footer">
        <div class="form-group">
            <label class="col-sm-2 control-label">当前目录</label>
            <div class="col-sm-10">
                <input disabled type="text" value="" class="form-control" name="remoteDir" title="目标地址">
            </div>
            <div class="clearfix"></div>
        </div>
    </div>

    <div class="modal-footer">
        <div class="form-group">
            <div class="col-sm-12 text-right">
                <button type="button" class="btn btnPrimary" onclick="doAddDoc();" style="width: 100px;height: 42px;">
                    确定
                </button>
                <button type="button" class="btn btnPrimary" onclick="cancelAddDoc();"
                        style="width: 100px;height: 42px;">取消
                </button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    function addDocPageInit(_type, _parentNode) {
        console.log("addDocPageInit()");
        AddDocConfirm.init(_type, _parentNode);
    }

    function closeAddDocDialog() {
        closeBootstrapDialog("addDoc");
    }

    function doAddDoc() {
        AddDocConfirm.doAddDoc();
        closeAddDocDialog();
    }

    function cancelAddDoc() {
        closeAddDocDialog();
    }

    var AddDocConfirm = (function () {
        var type;
        var parentNode;

        var parentId = 0;
        var parentPath = "";
        var remoteDir = "/html-client-container"; //only for display

        function init(_type, _parentNode) {
            console.log("AddDocConfirm.init()");

            type = _type;
            parentNode = _parentNode;
            if (parentNode && parentNode.id) {
                parentId = parentNode.id;
                parentPath = getNodePath(parentNode) + parentNode.name + "/";
                remoteDir += '/' + parentPath; //only for display
            }


            console.log(parentNode + "::::::::")
            console.log(remoteDir + "::::::::")


            $("#dialog-new-doc input[name='name']").focus();
            $("#dialog-new-doc input[name='remoteDir']").val(remoteDir);
        }

        function doAddDoc() {
            var name = $("#dialog-new-doc input[name='name']").val();
            var content = $("#dialog-new-doc input[name='content']").val();

            //检查当前目录下是否有同名文件
            if (isNodeExist(name, parentNode) == true) {
                showErrorMessage(name + " 已存在");
                return false;
            }
            //发送新建文件请求到后台
            if (name) {
                if (content && content != "") {
                    content = "#" + content;
                }

                addDoc(name, type, content, parentNode, parentId);
                return true;
            } else {
                alert("文件名不能为空");
                return false;
            }
        }

        function addDoc(name, type, content, parentNode, parentId) {
            console.log("addDoc name:" + name + " type:" + type + " content:" + content + " parentNode:" + parentNode);
            var parentPath = "";
            if (parentNode && parentNode != null) {
                parentPath = parentNode.path + "/";
            }

            $.ajax({
                url: "/doc/addDoc",
                type: "post",
                dataType: "json",
                data: {
                    type: type, //文件类型
                    pid: parentId,
                    path: parentPath,
                    name: name,
                    content: content,
                    shareId: gShareId,
                },
                success: function (ret) {
                    console.log("addDoc ret:", ret);
                    if (ret.status && ret.data.id) {
                        if (type == 2) {
                            isParent = true;
                        } else {
                            isParent = false;
                        }

                        //Add zTreeNode
                        addTreeNode(ret.data);

                        //Add docListNode
                        addDocListNode(ret.data);

                        // 普通消息提示条
                        bootstrapQ.msg({
                            msg: "新建完成！",
                            type: 'success',
                            time: 2000,
                        });
                    } else {
                        showErrorMessage("新建失败:" + ret.msgInfo);
                    }
                },
                error: function () {
                    showErrorMessage("新建失败:服务器异常！");
                }
            });
        }

        //开放给外部的调用接口
        return {
            init: function (_type, _parentNode) {
                init(_type, _parentNode);
            },
            doAddDoc: function () {
                doAddDoc();
            },
        };
    })();

</script>

